
package com.oet.persistence.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import com.oet.persistence.entity.*;
import com.oet.persistence.manager.OetDbMngr;
import com.oet.persistence.operator.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * JPA implementation of the Ydk Db interface using EntityManager.
 * <p>
 * The mappings are defined in "orm.xml" located in the META-INF directory.
 *
 * @author Zhang Yu
 */
@Repository
@Transactional
public class OetDbMngrImpl implements OetDbMngr{

	/** Logger that is available to subclasses
	private final Log logger = LogFactory.getLog(getClass().getName());
	 */
	
	@PersistenceContext
	private EntityManager em;
	
	public EntityManager getEntityManager() {
		// TODO Auto-generated method stub
		return em;
	}
	
	
	
	
	/**
	 * Retrieve all <code>Key</code>s from the data store.
	 * @return Collection<Key>
	 * @throws DataAccessException
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Key> getAllKeys() throws DataAccessException
	{
		KeyQuery query = new KeyQuery(em);
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Key> getLimitedKeys(Integer maxNumPerPage) throws DataAccessException
	{
		KeyQuery query = new KeyQuery(em);
		query.setMaxResults(maxNumPerPage);
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Key> findAllKeys(Object searchKey) throws DataAccessException
	{
		return null;
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Key> findLimitedKeys(Object searchKey, Integer maxNumPerPage) throws DataAccessException
	{
		return null;
	}
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public Key getKey(Object id) throws DataAccessException
	{
		KeyEntity entity = new KeyEntity(em);
		return entity.loadEntity(id);
	}
	
	@Transactional(readOnly = true)
	public Key getKeyByYdkBookId(String ydkBookId) throws DataAccessException
	{
		KeyQuery query = new KeyQuery(em);
		return query.searchKeyByYdkBookId(ydkBookId);
	}
	
	public void storeKey(Key instance) throws DataAccessException
	{
		KeyEntity entity = new KeyEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateKey(Key instance) throws DataAccessException
	{
		KeyEntity entity = new KeyEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteKey(Object id) throws DataAccessException
	{
		KeyEntity entity = new KeyEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * Retrieve all <code>YdkRole</code>s from the data store.
	 * @return Collection<YdkRole>
	 * @throws DataAccessException
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrRole> getAllYdkRoles() throws DataAccessException
	{
		MngrRoleQuery query = new MngrRoleQuery(em);
		//need to eager initialization
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrRole> getLimitedYdkRoles(Integer maxNumPerPage) throws DataAccessException
	{
		MngrRoleQuery query = new MngrRoleQuery(em);
		query.setMaxResults(maxNumPerPage);
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrRole> findAllYdkRoles(Object searchKey) throws DataAccessException
	{
		return null;
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrRole> findLimitedYdkRoles(Object searchKey, Integer maxNumPerPage) throws DataAccessException
	{
		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public MngrRole getYdkRole(Object id) throws DataAccessException
	{
		MngrRoleEntity entity = new MngrRoleEntity(em);
		return entity.loadEntity(id);
	}
	
	public void storeYdkRole(MngrRole instance) throws DataAccessException
	{
		MngrRoleEntity entity = new MngrRoleEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateYdkRole(MngrRole instance) throws DataAccessException
	{
		MngrRoleEntity entity = new MngrRoleEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteYdkRole(Object id) throws DataAccessException
	{
		MngrRoleEntity entity = new MngrRoleEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	/**
	 * Retrieve all <code>YdkAccount</code>s from the data store.
	 * @return Collection<YdkAccount>
	 * @throws DataAccessException
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrAccount> getAllYdkAccounts() throws DataAccessException
	{
		MngrAccountQuery query = new MngrAccountQuery(em);
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrAccount> getLimitedYdkAccounts(Integer maxNumPerPage) throws DataAccessException
	{
		MngrAccountQuery query = new MngrAccountQuery(em);
		query.setMaxResults(maxNumPerPage);
		return query.getResultList();
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrAccount> findAllYdkAccounts(Object searchKey) throws DataAccessException
	{
		return null;
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<MngrAccount> findLimitedYdkAccounts(Object searchKey, Integer maxNumPerPage) throws DataAccessException
	{
		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public MngrAccount getYdkAccount(Object id) throws DataAccessException
	{
		MngrAccountEntity entity = new MngrAccountEntity(em);
		return entity.loadEntity(id);
	}
	
	public MngrAccount getYdkAccountByName(String username)
		throws DataAccessException 
	{
		// TODO Auto-generated method stub
		MngrAccountQuery query = new MngrAccountQuery(em);
		return query.searchYdkAccountByUsername(username);
	}
	
	public void storeYdkAccount(MngrAccount instance) throws DataAccessException
	{
		MngrAccountEntity entity = new MngrAccountEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateYdkAccount(MngrAccount instance) throws DataAccessException
	{
		MngrAccountEntity entity = new MngrAccountEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteYdkAccount(Object id) throws DataAccessException
	{
		MngrAccountEntity entity = new MngrAccountEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity Category
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Category> getAllCategorys() throws DataAccessException
	{
		CategoryQuery query = new CategoryQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Category> getCategory(List<Long> ids) throws DataAccessException
	{
		CategoryQuery query = new CategoryQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCategory(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Category> getLimitedCategorys(Integer maxNumPerPage) throws DataAccessException
	{
		CategoryQuery query = new CategoryQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Category> findAllCategorys(Object searchKey) throws DataAccessException
	{
		CategoryQuery query = new CategoryQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Category> searchCategory(String name)
	{
		CategoryQuery query = new CategoryQuery(em);
		return query.searchCategoryByMultiField(name);
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public Category getCategory(Object id) throws DataAccessException
	{
		CategoryEntity entity = new CategoryEntity(em);
		return entity.loadEntity(id);
	}
	
	public Category getCategoryByTitle(String title) throws DataAccessException
	{
		CategoryQuery query = new CategoryQuery(em);
		return query.searchCategoryByTitle(title);
	}
	
	public void storeCategory(Category instance) throws DataAccessException
	{
		CategoryEntity entity = new CategoryEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCategory(Category instance) throws DataAccessException
	{
		CategoryEntity entity = new CategoryEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCategory(Object id) throws DataAccessException
	{
		CategoryEntity entity = new CategoryEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	/**
	 * For processing Entity Course
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Course> getAllCourses() throws DataAccessException
	{
		CourseQuery query = new CourseQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Course> getCourse(List<Long> ids) throws DataAccessException
	{
		CourseQuery query = new CourseQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCourse(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Course> getLimitedCourses(Integer maxNumPerPage) throws DataAccessException
	{
		CourseQuery query = new CourseQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Course> findAllCourses(Object searchKey) throws DataAccessException
	{
		CourseQuery query = new CourseQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Course> searchCourse(CourseSeries courseSeries,
			String name,  Integer chapterId, Integer type, 
			Integer level, Double payFactor, Long teacherId,
			String topic, Integer capacity, Integer occupyNum, Date startDate, Date endDate)
	{
		CourseQuery query = new CourseQuery(em);
		return query.searchCourseByMultiField(courseSeries,
				 name,   chapterId,  type, 
				 level,  payFactor,  teacherId,
				 topic,  capacity,  occupyNum,  startDate,  endDate);
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public Course getCourse(Object id) throws DataAccessException
	{
		CourseEntity entity = new CourseEntity(em);
		return entity.loadEntity(id);
	}
	
	public Course getCourseByTitle(String title) throws DataAccessException
	{
		CourseQuery query = new CourseQuery(em);
//		return query.searchCourseByTitle(title);
		return null;
	}
	
	public void storeCourse(Course instance) throws DataAccessException
	{
		CourseEntity entity = new CourseEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCourse(Course instance) throws DataAccessException
	{
		CourseEntity entity = new CourseEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCourse(Object id) throws DataAccessException
	{
		CourseEntity entity = new CourseEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity CourseFile
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseFile> getAllCourseFiles() throws DataAccessException
	{
		CourseFileQuery query = new CourseFileQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseFile> getCourseFile(List<Long> ids) throws DataAccessException
	{
		CourseFileQuery query = new CourseFileQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCourseFile(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseFile> getLimitedCourseFiles(Integer maxNumPerPage) throws DataAccessException
	{
		CourseFileQuery query = new CourseFileQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseFile> findAllCourseFiles(Object searchKey) throws DataAccessException
	{
		CourseFileQuery query = new CourseFileQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<CourseFile> searchCourseFile(Long courseId, Long courseSeriesId, String name, Integer type,
			Integer version, Date startDate, Date endDate)
	{
		CourseFileQuery query = new CourseFileQuery(em);
		return query.searchCourseFileByMultiField(courseId, courseSeriesId, name, type,
				version, startDate, endDate);
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public CourseFile getCourseFile(Object id) throws DataAccessException
	{
		CourseFileEntity entity = new CourseFileEntity(em);
		return entity.loadEntity(id);
	}
	
	public CourseFile getCourseFileByTitle(String title) throws DataAccessException
	{
		CourseFileQuery query = new CourseFileQuery(em);
		return query.searchCourseFileByTitle(title);
	}
	
	public void storeCourseFile(CourseFile instance) throws DataAccessException
	{
		CourseFileEntity entity = new CourseFileEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCourseFile(CourseFile instance) throws DataAccessException
	{
		CourseFileEntity entity = new CourseFileEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCourseFile(Object id) throws DataAccessException
	{
		CourseFileEntity entity = new CourseFileEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	
	/**
	 * For processing Entity CourseNews
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseNews> getAllCourseNewss() throws DataAccessException
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseNews> getCourseNews(List<Long> ids) throws DataAccessException
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCourseNews(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseNews> getLimitedCourseNewss(Integer maxNumPerPage) throws DataAccessException
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseNews> findAllCourseNewss(Object searchKey) throws DataAccessException
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<CourseNews> searchCourseNews(
			Long courseId, Long courseSeriesId, String name, Integer type,
			Integer version, Date startDate, Date endDate)
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
		return query.searchCourseNewsByMultiField(courseId, courseSeriesId, name, type,
				version, startDate, endDate);
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public CourseNews getCourseNews(Object id) throws DataAccessException
	{
		CourseNewsEntity entity = new CourseNewsEntity(em);
		return entity.loadEntity(id);
	}
	
	public CourseNews getCourseNewsByTitle(String title) throws DataAccessException
	{
		CourseNewsQuery query = new CourseNewsQuery(em);
//		return query.searchCourseNewsByTitle(title);
		return null;
	}
	
	public void storeCourseNews(CourseNews instance) throws DataAccessException
	{
		CourseNewsEntity entity = new CourseNewsEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCourseNews(CourseNews instance) throws DataAccessException
	{
		CourseNewsEntity entity = new CourseNewsEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCourseNews(Object id) throws DataAccessException
	{
		CourseNewsEntity entity = new CourseNewsEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity CourseSeries
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseSeries> getAllCourseSeriess() throws DataAccessException
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseSeries> getCourseSeries(List<Long> ids) throws DataAccessException
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCourseSeries(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseSeries> getLimitedCourseSeriess(Integer maxNumPerPage) throws DataAccessException
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<CourseSeries> findAllCourseSeriess(Object searchKey) throws DataAccessException
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<CourseSeries> searchCourseSeries(
			String name, Category category, 
			Integer level, Integer courseNumber, Teacher teacher,
			Integer capacity, Integer occupyNum, Date startDate, Date endDate, 
			Boolean isActived, Double totalPrice)
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		return query.searchCourseSeriesByMultiField(name, category, 
				level, courseNumber, teacher,
				capacity, occupyNum, startDate, endDate, 
				isActived, totalPrice, null, null, null, null);
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<CourseSeries> searchCourseSeries(
			String name, Category category, 
			Integer level, Integer courseNumber, Teacher teacher,
			Integer capacity, Integer occupyNum, Date startDate, Date endDate, 
			Boolean isActived, Double totalPrice, Integer weekdayFlagStart, 
			Integer weekdayFlagEnd, Integer timeFlag, Boolean isAvailable)
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
		return query.searchCourseSeriesByMultiField(name, category, 
				level, courseNumber, teacher,
				capacity, occupyNum, startDate, endDate, 
				isActived, totalPrice,weekdayFlagStart, 
				weekdayFlagEnd,timeFlag, isAvailable);
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public CourseSeries getCourseSeries(Object id) throws DataAccessException
	{
		CourseSeriesEntity entity = new CourseSeriesEntity(em);
		return entity.loadEntity(id);
	}
	
	public CourseSeries getCourseSeriesByTitle(String title) throws DataAccessException
	{
		CourseSeriesQuery query = new CourseSeriesQuery(em);
//		return query.searchCourseSeriesByTitle(title);
		return null;
	}
	
	public void storeCourseSeries(CourseSeries instance) throws DataAccessException
	{
		//Here we need to update the weekday/weekend flag and time flag
		
		if (instance.getStartDate() != null)
		{
			Calendar c = Calendar.getInstance();
			c.setTime(instance.getStartDate());
			
			int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
			instance.setWeekdayFlag(dayOfWeek - 1);
			
			if (instance.getWeekdayFlag() == 0)
			{
				instance.setWeekdayFlag(CourseSeries.WEEKDAY_FLAG_SUN);
			}
			
			int hour = c.get(Calendar.HOUR_OF_DAY);
			if (hour > 0 && hour < 12)
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_BEFORENOON);
			}
			else if (hour >=12 && hour <=6)
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_AFTERENOON);
			}
			else
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_NIGHT);
			}
		}
		CourseSeriesEntity entity = new CourseSeriesEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCourseSeries(CourseSeries instance) throws DataAccessException
	{
		
		if (instance.getStartDate() != null)
		{
			Calendar c = Calendar.getInstance();
			c.setTime(instance.getStartDate());
			
			int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
			instance.setWeekdayFlag(dayOfWeek - 1);
			
			if (instance.getWeekdayFlag() == 0)
			{
				instance.setWeekdayFlag(CourseSeries.WEEKDAY_FLAG_SUN);
			}
			int hour = c.get(Calendar.HOUR_OF_DAY);
			if (hour > 0 && hour < 12)
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_BEFORENOON);
			}
			else if (hour >=12 && hour <=6)
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_AFTERENOON);
			}
			else
			{
				instance.setTimeFlag(CourseSeries.TIME_FLAG_NIGHT);
			}
		}
		CourseSeriesEntity entity = new CourseSeriesEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCourseSeries(Object id) throws DataAccessException
	{
		CourseSeriesEntity entity = new CourseSeriesEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	
	/**
	 * For processing Entity Customer
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Customer> getAllCustomers() throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Customer> getCustomer(List<Long> ids) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchCustomer(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Customer> getLimitedCustomers(Integer maxNumPerPage) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Customer> findAllCustomers(Object searchKey) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Customer> searchCustomer(String name, String email, String mobilePhone, 
			Integer level, Integer couresHours, Boolean isActived, Integer totalCourseNumber,
			Boolean isTestFinished, Long accountId)
	{
		CustomerQuery query = new CustomerQuery(em);
		return query.searchCustomerByMultiField(name, email, mobilePhone, 
				level, couresHours, isActived, totalCourseNumber,
				isTestFinished, accountId);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public Customer getCustomer(Object id) throws DataAccessException
	{
		CustomerEntity entity = new CustomerEntity(em);
		return entity.loadEntity(id);
	}
	
	public Customer getCustomerByTitle(String title) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		return query.searchEqualByFiled("name", title);
	}
	public Customer getCustomerByEmail(String email) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		return query.searchEqualByFiled("email", email);
	}
	public Customer getCustomerByTel(String tel) throws DataAccessException
	{
		CustomerQuery query = new CustomerQuery(em);
		return query.searchEqualByFiled("mobilePhone", tel);
	}
	
	public void storeCustomer(Customer instance) throws DataAccessException
	{
		CustomerEntity entity = new CustomerEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateCustomer(Customer instance) throws DataAccessException
	{
		CustomerEntity entity = new CustomerEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteCustomer(Object id) throws DataAccessException
	{
		CustomerEntity entity = new CustomerEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity Teacher
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Teacher> getAllTeachers() throws DataAccessException
	{
		TeacherQuery query = new TeacherQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Teacher> getTeacher(List<Long> ids) throws DataAccessException
	{
		TeacherQuery query = new TeacherQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchTeacher(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Teacher> getLimitedTeachers(Integer maxNumPerPage) throws DataAccessException
	{
		TeacherQuery query = new TeacherQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<Teacher> findAllTeachers(Object searchKey) throws DataAccessException
	{
		TeacherQuery query = new TeacherQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Teacher> searchTeacher(String name, String email, String mobilePhone, 
			Integer level, Boolean isActived, Integer totalCourseNumber)
	{
		TeacherQuery query = new TeacherQuery(em);
		return query.searchTeacherByMultiField(name, email, mobilePhone, 
				level, isActived, totalCourseNumber);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public Teacher getTeacher(Object id) throws DataAccessException
	{
		TeacherEntity entity = new TeacherEntity(em);
		return entity.loadEntity(id);
	}
	
	public Teacher getTeacherByTitle(String title) throws DataAccessException
	{
		TeacherQuery query = new TeacherQuery(em);
		List<Teacher> list = query.searchTeacherByMultiField(title, null, null, null, null, null);
		if (list != null && list.size() > 0)
		{
			return list.get(0);
		}
		else
		{
			return null;
		}
	}
	
	public void storeTeacher(Teacher instance) throws DataAccessException
	{
		TeacherEntity entity = new TeacherEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateTeacher(Teacher instance) throws DataAccessException
	{
		TeacherEntity entity = new TeacherEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteTeacher(Object id) throws DataAccessException
	{
		TeacherEntity entity = new TeacherEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity UserComment
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserComment> getAllUserComments() throws DataAccessException
	{
		UserCommentQuery query = new UserCommentQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserComment> getUserComment(List<Long> ids) throws DataAccessException
	{
		UserCommentQuery query = new UserCommentQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchUserComment(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserComment> getLimitedUserComments(Integer maxNumPerPage) throws DataAccessException
	{
		UserCommentQuery query = new UserCommentQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserComment> findAllUserComments(Object searchKey) throws DataAccessException
	{
		UserCommentQuery query = new UserCommentQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<UserComment> searchUserComment(
			Long courseId, Long courseSeriesId, Long teacherId, 
			String topic, Integer type, Integer level, 
			Date startDate, Date endDate, Customer customer)
	{
		UserCommentQuery query = new UserCommentQuery(em);
		return query.searchUserCommentByMultiField(
				courseId, courseSeriesId, teacherId, 
				topic, type, level, 
				startDate, endDate, customer);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public UserComment getUserComment(Object id) throws DataAccessException
	{
		UserCommentEntity entity = new UserCommentEntity(em);
		return entity.loadEntity(id);
	}
	
	public UserComment getUserCommentByTitle(String title) throws DataAccessException
	{
		UserCommentQuery query = new UserCommentQuery(em);
//		return query.searchUserCommentByTitle(title);
		return null;
	}
	
	public void storeUserComment(UserComment instance) throws DataAccessException
	{
		UserCommentEntity entity = new UserCommentEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateUserComment(UserComment instance) throws DataAccessException
	{
		UserCommentEntity entity = new UserCommentEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteUserComment(Object id) throws DataAccessException
	{
		UserCommentEntity entity = new UserCommentEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity UserCourseSeries
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserCourseSeries> getAllUserCourseSeriess() throws DataAccessException
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserCourseSeries> getUserCourseSeries(List<Long> ids) throws DataAccessException
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchUserCourseSeries(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserCourseSeries> getLimitedUserCourseSeriess(Integer maxNumPerPage) throws DataAccessException
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserCourseSeries> findAllUserCourseSeriess(Object searchKey) throws DataAccessException
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<UserCourseSeries> searchUserCourseSeries(
			Long courseSeriesId, String name,  
			Integer type, Integer level, Long teacherId, String teacherName,Integer capacity,
			Date startDate, Date endDate, Customer customer)
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
		return query.searchUserCourseSeriesByMultiField(
				courseSeriesId, name,  
				type, level, teacherId, teacherName,capacity,
				startDate, endDate, customer);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public UserCourseSeries getUserCourseSeries(Object id) throws DataAccessException
	{
		UserCourseSeriesEntity entity = new UserCourseSeriesEntity(em);
		return entity.loadEntity(id);
	}
	
	public UserCourseSeries getUserCourseSeriesByTitle(String title) throws DataAccessException
	{
		UserCourseSeriesQuery query = new UserCourseSeriesQuery(em);
//		return query.searchUserCourseSeriesByTitle(title);
		return null;
	}
	
	public void storeUserCourseSeries(UserCourseSeries instance) throws DataAccessException
	{
		UserCourseSeriesEntity entity = new UserCourseSeriesEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateUserCourseSeries(UserCourseSeries instance) throws DataAccessException
	{
		UserCourseSeriesEntity entity = new UserCourseSeriesEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteUserCourseSeries(Object id) throws DataAccessException
	{
		UserCourseSeriesEntity entity = new UserCourseSeriesEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	
	/**
	 * For processing Entity UserHistoryCourse
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserHistoryCourse> getAllUserHistoryCourses() throws DataAccessException
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserHistoryCourse> getUserHistoryCourse(List<Long> ids) throws DataAccessException
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchUserHistoryCourse(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserHistoryCourse> getLimitedUserHistoryCourses(Integer maxNumPerPage) throws DataAccessException
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserHistoryCourse> findAllUserHistoryCourses(Object searchKey) throws DataAccessException
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<UserHistoryCourse> searchUserHistoryCourse(
			Long courseId, Long courseSeriesId, String name,  
			Integer type, Integer level, Double payFactor, Long teacherId, String teacherName,
			String topic, Integer capacity,
			Date startDate, Date endDate, Boolean isCommented,Customer customer)
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
		return query.searchUserHistoryCourseByMultiField(
				courseId, courseSeriesId, name,  
				type, level, payFactor, teacherId, teacherName,
				topic, capacity,
				startDate, endDate, isCommented,customer);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public UserHistoryCourse getUserHistoryCourse(Object id) throws DataAccessException
	{
		UserHistoryCourseEntity entity = new UserHistoryCourseEntity(em);
		return entity.loadEntity(id);
	}
	
	public UserHistoryCourse getUserHistoryCourseByTitle(String title) throws DataAccessException
	{
		UserHistoryCourseQuery query = new UserHistoryCourseQuery(em);
//		return query.searchUserHistoryCourseByTitle(title);
		return null;
	}
	
	public void storeUserHistoryCourse(UserHistoryCourse instance) throws DataAccessException
	{
		UserHistoryCourseEntity entity = new UserHistoryCourseEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateUserHistoryCourse(UserHistoryCourse instance) throws DataAccessException
	{
		UserHistoryCourseEntity entity = new UserHistoryCourseEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteUserHistoryCourse(Object id) throws DataAccessException
	{
		UserHistoryCourseEntity entity = new UserHistoryCourseEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * For processing Entity UserPlannedCourse
	 * 
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserPlannedCourse> getAllUserPlannedCourses() throws DataAccessException
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserPlannedCourse> getUserPlannedCourse(List<Long> ids) throws DataAccessException
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
		if (ids == null || ids.size() == 0)
		{
			query.setOrderDesc("id");
			return query.getResultList();
		}
		else
		{
			return query.searchUserPlannedCourse(ids);
		}
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserPlannedCourse> getLimitedUserPlannedCourses(Integer maxNumPerPage) throws DataAccessException
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
		query.setMaxResults(maxNumPerPage);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<UserPlannedCourse> findAllUserPlannedCourses(Object searchKey) throws DataAccessException
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
		query.addRestrictionParameter("name", searchKey);
		query.setOrderDesc("id");
		return query.getResultList();
	}
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<UserPlannedCourse> searchUserPlannedCourse(
			Long courseId, Long courseSeriesId, String name,  
			Integer type, Integer level, Double payFactor, Long teacherId, String teacherName,
			String topic, Integer capacity,
			Date startDate, Date endDate, Customer customer)
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
		return query.searchUserPlannedCourseByMultiField(
				courseId, courseSeriesId, name,  
				type, level, payFactor, teacherId, teacherName,
				topic, capacity,
				startDate, endDate, customer);
//		return null;
	}
	
	/**
	 * CRUD
	 */
	@Transactional(readOnly = true)
	public UserPlannedCourse getUserPlannedCourse(Object id) throws DataAccessException
	{
		UserPlannedCourseEntity entity = new UserPlannedCourseEntity(em);
		return entity.loadEntity(id);
	}
	
	public UserPlannedCourse getUserPlannedCourseByTitle(String title) throws DataAccessException
	{
		UserPlannedCourseQuery query = new UserPlannedCourseQuery(em);
//		return query.searchUserPlannedCourseByTitle(title);
		return null;
	}
	
	public void storeUserPlannedCourse(UserPlannedCourse instance) throws DataAccessException
	{
		UserPlannedCourseEntity entity = new UserPlannedCourseEntity(em);
		entity.setInstance(instance);
		entity.persist();
	}
	public void updateUserPlannedCourse(UserPlannedCourse instance) throws DataAccessException
	{
		UserPlannedCourseEntity entity = new UserPlannedCourseEntity(em);
		entity.setInstance(instance);
		entity.update();
	}
	public void deleteUserPlannedCourse(Object id) throws DataAccessException
	{
		UserPlannedCourseEntity entity = new UserPlannedCourseEntity(em);
		if (entity.loadEntity(id) != null)
		{
			entity.remove();
		}
	}
	
	
	/**
	 * 
	 * Example For New Entity
	 * 
	 */
//	/**
//	 * Retrieve all <code>Channel</code>s from the data store.
//	 * @return Collection<Channel>
//	 * @throws DataAccessException
//	 */
//	@Transactional(readOnly = true)
//	@SuppressWarnings("unchecked")
//	public Collection<Channel> getAllChannels() throws DataAccessException
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		query.setOrderDesc("channel_order");
//		return query.getResultList();
//	}
//	@Transactional(readOnly = true)
//	@SuppressWarnings("unchecked")
//	public Collection<Channel> getChannel(List<Long> ids) throws DataAccessException
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		if (ids == null || ids.size() == 0)
//		{
//			query.setOrderDesc("channel_order");
//			return query.getResultList();
//		}
//		else
//		{
//			return query.searchChannel(ids);
//		}
//	}
//	@Transactional(readOnly = true)
//	@SuppressWarnings("unchecked")
//	public Collection<Channel> getLimitedChannels(Integer maxNumPerPage) throws DataAccessException
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		query.setMaxResults(maxNumPerPage);
//		query.setOrderDesc("channel_order");
//		return query.getResultList();
//	}
//	@Transactional(readOnly = true)
//	@SuppressWarnings("unchecked")
//	public Collection<Channel> findAllChannels(Object searchKey) throws DataAccessException
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		query.addRestrictionParameter("name", searchKey);
//		query.setOrderDesc("channel_order");
//		return query.getResultList();
//	}
//	@Transactional(readOnly = true)
//	@SuppressWarnings("unchecked")
//	public List<Channel> searchChannel(String title, Integer category, Integer type, Long parentId)
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		return query.searchChannelByMultiField(title, category, type, parentId);
//	}
//	
//	/**
//	 * CRUD
//	 */
//	@Transactional(readOnly = true)
//	public Channel getChannel(Object id) throws DataAccessException
//	{
//		ChannelEntity entity = new ChannelEntity(em);
//		return entity.loadEntity(id);
//	}
//	
//	public Channel getChannelByTitle(String title) throws DataAccessException
//	{
//		ChannelQuery query = new ChannelQuery(em);
//		return query.searchChannelByTitle(title);
//	}
//	
//	public void storeChannel(Channel instance) throws DataAccessException
//	{
//		ChannelEntity entity = new ChannelEntity(em);
//		entity.setInstance(instance);
//		entity.persist();
//	}
//	public void updateChannel(Channel instance) throws DataAccessException
//	{
//		ChannelEntity entity = new ChannelEntity(em);
//		entity.setInstance(instance);
//		entity.update();
//	}
//	public void deleteChannel(Object id) throws DataAccessException
//	{
//		ChannelEntity entity = new ChannelEntity(em);
//		if (entity.loadEntity(id) != null)
//		{
//			entity.remove();
//		}
//	}
	
	
	
	
}
